using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using FzWeb.Models;
using FzWeb.Filter;
namespace FzWeb.Controllers
{
    [ExceptionFilter]
    public class CompanyTypeController : Controller
    {

        private BaseDataService bds = new BaseDataService();
        public ActionResult Index()
        {
            TempData["maxOrder"] = bds.GetCompanyTypeList().Last().Order + 1;
            return View(bds.GetCompanyTypeList());
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(CompanyType e)
        {

            e.Order = (int)TempData["maxOrder"];
            bds.CreateCompanyType(e);
            return RedirectToAction("Index");

        }



        public ActionResult Edit(int id)
        {
            return View(bds.GetCompanyType(c => c.Id == id));
        }



        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(CompanyType e)
        {
            bds.UpdateCompanyType(e);

            return RedirectToAction("Index");

        }

        public ActionResult Delete(int id)
        {

            bds.DeleteCompanyType(bds.GetCompanyType(c => c.Id == id));
            return RedirectToAction("Index");

        }

        public ActionResult Up(int id)
        {
            CompanyType e = bds.GetCompanyType(c => c.Id == id);
            if (e.Order > 1)
            {
                CompanyType before = bds.GetBeforeCompanyType(e.Order);
                int temp = e.Order;
                e.Order = before.Order;
                before.Order = temp;
                bds.UpdateCompanyType(e);
                bds.UpdateCompanyType(before);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Down(int id)
        {
            CompanyType e = bds.GetCompanyType(c => c.Id == id);
            if (e.Order < (int)TempData["maxOrder"] - 1)
            {
                CompanyType after = bds.GetAfterCompanyType(e.Order);
                int temp = e.Order;
                e.Order = after.Order;
                after.Order = temp;
                bds.UpdateCompanyType(e);
                bds.UpdateCompanyType(after);
            }
            return RedirectToAction("Index");
        }

    }
}
